import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

# if __name__ == '__main__':
sendAddr = '772084279@qq.com'
sendPassword = 'dogdrgbemicjbbge'
toaddrs = ['772084279@qq.com', 'wangyong@xkw.cn']

# content = 'hello, this is email content.'
# textApart = MIMEText(content)
#     content = """
#         <head>
#     <meta charset="UTF-8">
#     <title>Title</title>
# </head>
# <body>
# <ul>更新内容
#     <li>学期前制定年度教学计划，安排各课时主备人</li>
#     <li>每周1～2次教研会，讨论本周备课课时的教学目标、重难点等</li>
#     <li>主备人根据会上结论，制作课件</li>
#     <li>完成后，将其共享在群里，大家线上/线下讨论定稿</li>
#     <li>其他教师根据自己学生学情修改课件，以适应本班教学；</li>
#     <li>备课成果性资料，一般会共享在QQ/微信群中</li>
# </ul>
# <ul>需求目标
#     <li>完成教师个人完成初稿——共享文档——集体备课——定稿——其他老师复制使用的协同备课的流程闭环</li>
#     <li>支持多人协作文档，设置协作人员，记录历史记录</li>
#     <li>协作动态能及时通知到所有协同者</li>
#     <li>预留学科组长等管理角色的教学任务发布和全员消息通知的功能区域</li>
# </ul>
# </body>
# </html>
#     """
#     textApart = MIMEText(content,'html',"utf-8")

# 发送图片附件
# imageFile = '微信图片_20200916085524.png'
# imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])
# imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile)

# 发送文本附件
pdfFile = 'MA1004测量审核样品软件需求规格说明书(1).pdf'
pdfApart = MIMEApplication(open(pdfFile, 'rb').read())
pdfApart.add_header('Content-Disposition', 'attachment', filename=pdfFile)

htmlFile = '自动化回归_2020-10-24_23_43_11_result.html'
htmlApart = MIMEApplication(open(htmlFile, 'rb').read())
htmlApart.add_header('Content-Disposition', 'attachment', filename=htmlFile)

# 内容发送HTML
textApart = MIMEText(open(htmlFile, 'rb').read(), 'html', "utf-8")

m = MIMEMultipart()
m.attach(textApart)
# m.attach(imageApart)
m.attach(pdfApart)
m.attach(htmlApart)
m['Subject'] = 'python 自动发送邮件'
m['From'] = sendAddr
# print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
m['Date'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

try:
    server = smtplib.SMTP('smtp.qq.com')
    server.login(sendAddr, sendPassword)
    server.sendmail(sendAddr, toaddrs, m.as_string())
    print('success')
    server.quit()
except smtplib.SMTPException as e:
    print('error:', e)  # 打印错误
